package com.sdry.service.impl.zc;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.sdry.mapper.zc.ZcBindAndUnbindMapper;
import com.sdry.model.zc.ZcMaterielAndTrayEntity;
import com.sdry.service.zc.ZcBindAndUnbindService;

/**
 * 物料托盘绑定与解绑
 * @ClassName:    ZcBindAndUnbindServiceImpl   
 * @Description:  
 * @Author:       zc   
 * @CreateDate:   2019年6月3日 上午10:57:42   
 * @Version:      v1.0
 */
@Service
public class ZcBindAndUnbindServiceImpl implements ZcBindAndUnbindService{

	@Resource
	private ZcBindAndUnbindMapper zcBindAndUnbindMapper;
	/**
	 * 物料托盘绑定
	 */
	@Override
	public int bindingMaterielAndTray(ZcMaterielAndTrayEntity zcMaterielAndTrayEntity) {
		String[] materiel_code_arr = zcMaterielAndTrayEntity.getMateriel_code().split(",");
		String size = zcMaterielAndTrayEntity.getSizeAndPre().split("/")[0];
		String pre = zcMaterielAndTrayEntity.getSizeAndPre().split("/")[1];
		int code_num = materiel_code_arr.length;
		zcMaterielAndTrayEntity.setCode_num(code_num);
		zcMaterielAndTrayEntity.setMateriel_size(size);
		zcMaterielAndTrayEntity.setMateriel_properties(pre);
		ZcMaterielAndTrayEntity zcMaterielAndTrayEntityOld = zcBindAndUnbindMapper.selectMaterielAndTray(zcMaterielAndTrayEntity);
		int affact = 0;
		if(zcMaterielAndTrayEntityOld == null){
			affact = zcBindAndUnbindMapper.bindingMaterielAndTray(zcMaterielAndTrayEntity);
		}
		else{
			Long mid = zcBindAndUnbindMapper.selectMidByNewCode(zcMaterielAndTrayEntity);
			if(zcMaterielAndTrayEntityOld.getMid() != mid){
				affact = -2;
			}else{
				affact = zcBindAndUnbindMapper.addMaterielAndTray(zcMaterielAndTrayEntity);
			}
		}
		return affact;
	}
	/**
	 * 解绑
	 */
	@Override
	public int unbindMaterielAndTray(ZcMaterielAndTrayEntity zcMaterielAndTrayEntity) {
		ZcMaterielAndTrayEntity zcMaterielAndTrayEntityOld = zcBindAndUnbindMapper.selectMaterielCodeByTrayCode(zcMaterielAndTrayEntity);
		if(zcMaterielAndTrayEntityOld==null){
			return -2;
		}
		String[] materiel_code_old = zcMaterielAndTrayEntityOld.getMateriel_code().split(",");
		String[] materiel_code_new = zcMaterielAndTrayEntity.getMateriel_code().split(",");
		String new_materiel_code_list="";
		for(int i = 0; i < materiel_code_old.length; i++){
			for(int j = 0; j < materiel_code_new.length; j++){
				if(materiel_code_new[j].equals(materiel_code_old[i])){
					materiel_code_old = org.apache.commons.lang3.ArrayUtils.remove(materiel_code_old, i);
				}
			}
		}
		for (String string : materiel_code_old) {
			new_materiel_code_list += string+",";
		}
		zcMaterielAndTrayEntity.setNew_materiel_code_list(new_materiel_code_list);
		zcMaterielAndTrayEntity.setPackType(zcMaterielAndTrayEntityOld.getPackType());
		zcMaterielAndTrayEntity.setCode_num(materiel_code_new.length);
		zcMaterielAndTrayEntity.setMid(zcMaterielAndTrayEntityOld.getMid());
		return zcBindAndUnbindMapper.unbindMaterielAndTray(zcMaterielAndTrayEntity);
	}
	
}
